<?php
// 命名空间定义
namespace app\api\controller;
 
use app\api\model\Identity;
use app\api\model\User;
use app\common\controller\ApiController;
use think\App;
use think\facade\Env;
// use app\admin\service\ConfigService;
use app\BaseController;
use app\common\constants\AdminConstant;
use app\common\service\AuthService;
use EasyAdmin\tool\CommonTool;
use think\facade\View;
use think\Model;
use think\Request;
use think\facade\Config;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class Index extends ApiController
{
    public function __construct(App $app)
    {
        parent::__construct($app);

        // 初始化时自定义方法

    }

    public function index(){
        // return $this->request->param('name');
        return json([
            'code' => 1,
            'msg' => '测试成功',
            'params' => $this->request->param(''),
        ]);
    }
    public function logout(){
        $userModel = new User();
        $identityModel = new Identity();
        $identity = $this->identity; 
        try {
            $userModel->where('id', $identity['user_id'])->update(['identity_id'=>0]);
            $identityModel->where('id', $identity['id'])->update(['user_id'=>0]);
        }catch(\Exception $e){
            $userModel->rollback();
            return msg(100,'退出登录失败','');
        }
        $userModel->commit();
        return msg(200,'退出登录成功','');
    }
    public function checktoken(){
                //验证登录
        $token = $this->request->header('x-access-token');
        // var_dump($token); exit;
        if(!isset($token) || $token == ''){
            return msg(100,'失败','');
        }
        // 验证身份
        $identity = JWT::decode($token, new Key(config('app.jwt.key'), 'HS256'));
        $identityInfo = (new \app\api\model\Identity())->identityInfo(objToArray($identity));
        if(!$identityInfo){
            return msg(100,'失败','');
        }
        return msg(200,'成功','');
    }
}